//
//  DesDataObj.m
//  HappyTrip
//
//  Created by yuxuesong on 15-4-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DesDataObj.h"

@implementation DesDataObj

+ (DesDataObj *)shareInstance
{
    static DesDataObj *data = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [[DesDataObj alloc] init];
        [data openHappyTrip];
        [data createSpotTabel];
    });
    return data;
}

- (void)judgeRusult:(int)result type:(NSString *)type
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", type);
    } else {
        NSLog(@"%@失败, 失败编号: %d", type, result);
    }
}

//------ 打开数据库
- (void)openHappyTrip
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *happyTrip = [path stringByAppendingPathComponent:@"/HappyTrip.db"];
    NSLog(@"\n\n\n打开目的地数据库:\n\n  %@  \n\n\n", happyTrip);
    int result = sqlite3_open([happyTrip UTF8String], &happyTripPoint);
    [self judgeRusult:result type:@"打开数据库"];
}
//------ 关闭数据库
- (void)closeHappyTrip
{
    int result = sqlite3_close(happyTripPoint);
    [self judgeRusult:result type:@"关闭数据库"];
}
//------- 创建表
- (void)createSpotTabel
{
    NSString *sqlStr = @"create table spot (idNumber text primary key, typeNumber text, name text)";
    int result = sqlite3_exec(happyTripPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeRusult:result type:@"创建表"];
    NSLog(@"%@", sqlStr);
}
//------- 增加数据
- (void)insertSpot:(DestinationDetailObject *)spot
{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into spot values('%@', '%@', '%@')", spot.idNumber, spot.typeNumber, spot.name];
    int result = sqlite3_exec(happyTripPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeRusult:result type:@"添加数据"];
}
//------ 删除数据
- (void)deleteSpot:(DestinationDetailObject *)spot
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from spot where idNumber = '%@'", spot.idNumber];
    int result = sqlite3_exec(happyTripPoint, [sqlStr UTF8String], NULL, NULL, NULL);
    [self judgeRusult:result type:@"删除数据"];
}
//------- 查询所有数据
- (NSMutableArray *)selectAllSpots
{
    NSString *sqlStr = @"select * from spot";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(happyTripPoint, [sqlStr UTF8String], -1, &stmt, NULL);
    NSMutableArray *spotsArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        
        // 遍历结果中的所有数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            // 每一条数据都对应一个 spot 对象
            DestinationDetailObject *detailObj = [[DestinationDetailObject alloc] init];
            
            detailObj.idNumber = (NSNumber *)[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            detailObj.typeNumber = (NSNumber *)[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            detailObj.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            [spotsArr addObject:detailObj];
            [detailObj release];
        }
    }
    sqlite3_finalize(stmt);
    return spotsArr;
}



@end
